using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Money.Plugins.Import.RbsImport
{
    // TODO this needs a better name and code refactored where used
    public class TransactionMatch
    {
        private int _priority = 100;

        public int? CategoryId { get; set; }
        public decimal? MinimumAmount { get; set; }
        public decimal? MaximumAmount { get; set; }
        public string RegularExpression { get; set; }
        public Transaction.TransactionType? Type { get; set; }
        public int Priority 
        {
            get
            {
                // HACK we do not want any values of 0 for priority
                if (_priority == 0)
                {
                    _priority = 100;
                }

                return _priority;
            }
            set { _priority = value; } 
        }
        public bool Terminate { get; set; }

        public bool IsMatch(RbsTransaction transaction)
        {
            bool match = true;

            if (RegularExpression != null)
            {
                if (!Regex.IsMatch(transaction.Description, RegularExpression, RegexOptions.IgnoreCase))
                {
                    match = false;
                }
            }

            if (MinimumAmount != null)
            {
                if (transaction.Amount < MinimumAmount)
                {
                    match = false;
                }
            }

            if (MaximumAmount != null)
            {
                if (transaction.Amount > MaximumAmount)
                {
                    match = false;
                }
            }

            return match;
        }

        public TransactionMatch()
        {
            Terminate = false;

            _priority = 100;
        }

        public TransactionMatch(string regularExpression, int categoryId)
        {
            RegularExpression = regularExpression;
            CategoryId = categoryId;
            _priority = 100;
        }
    }
}
